package com.example.demo.service;


import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    // 通过 jobNum 查询用户
    public User findByJobNum(Integer jobNum) {
        return userRepository.findByJobNum(jobNum);
    }

    // 更新用户字段
    @Transactional
    public boolean updateUser(Integer jobNum, String uname, String phone, String address, String profession) {

        // 获取用户
            User user = userRepository.findByJobNum(jobNum);
            // 更新字段
            if (uname != null) user.setUname(uname);
            if (phone != null) user.setPhone(phone);
            if (address != null) user.setAddress(address);
            if (profession != null) user.setProfession(profession);
            // 保存更新后的用户

            try {
                userRepository.save(user);
                return true;
            }catch (Exception e){
                return false;
            }

    }

}



